
$(function(){
	
	var baseUrl = $("base").attr("href");
	var isEnd = false;
	var map;
	
	var checkResult = {
					"已通过":"<span class=\"label label-success\">",
					"未通过":"<span class=\"label label-danger\">",
					"待审核":"<span class=\"label label-warning\">",
				}
	var loadDataDefault = {
				url : baseUrl + "commenUtil/json-getUserEstateListByCheckstatePublicstatu.do",
				data:{pageSize:5,pageNum:1,estateInfoCheckState:"已通过",publicStatu : "待发布"},
				curPage : 1,
			    pageSize : 5,
	            maxSize : 0,
	}
				
	
	var $_modifyNode;
	
	
	/** 请求数据列表显示在table中 开始  */
	
	function loadData(){
		$.ajax({
		type:"post",
		url: loadDataDefault.url,
		async:true,
		data:loadDataDefault.data,
		success:function(data){
			if(data.code == 1000){
				$("section table tbody").html("");
				console.log("estateInfoList",data);
				estatInfos = JSON.parse(data.data);
				var i=0;
				if(!isEnd && estatInfos.length > 0){
					loadDataDefault.maxSize = loadDataDefault.curPage;
					if(estatInfos.length < loadDataDefault.pageSize){
						isEnd = true;
					}
				}else{
					isEnd = true;
					
				}
				for ( i; i < estatInfos.length; i++) {
					var item = estatInfos[i];
					var htmlStr =   
					"<tr >"+
					"	<td>" + item.id + "</td>"+
					"	<td>"+ item.user.realName +"</td>"+
					"	<td>"+ checkResult[item.estateInfoCheckState] +"<span class=\"glyphicon glyphicon-tags\"></span><span class=\"estateInfoCheckState\">" + item.estateInfoCheckState +"</span></span></td>"+
					"	<td>"+ item.estateType +"</td>"+
					"	<td class=\"btn-group\">"+
					"	<button class=\"btn btn-info\">查看信息</button>"+
					"	<button class=\"btn btn-success\">发布租售信息</button>"+
					"	<button class=\"btn btn-default btn-map\">位置</button>"+
					"	<button class=\"btn btn-warning\">添加描述图片</button>"+
					"	</td>"+
					"	</tr>";
					$(htmlStr).appendTo($("section tbody")).data("estatebasicinfo",item);
				}
			}else{
				console.log("loadDataError",data);
				sweetAlert({
				title:"错误",
				text:"系统错误",
				type:"error",
				timer:2000,
			});
			}
		},
		error:function(){
			sweetAlert({
				title:"错误",
				text:"连接服务器请求数据失败",
				type:"error",
				timer:2000,
			});
			
		}
		
	});
	}
	
	/** 请求数据列表显示在table中 结束  */
	
	/* 动态html事件绑定  开始*/
		/**房产信息显示*/
	$("section table").on("click",".btn-info",function(){
		var basicInfo = $(this).parents("tr").data("estatebasicinfo");
		console.log("estateBasicInfo",basicInfo);
		$(".estateID").eq(0).text(basicInfo.id);
		$("#owner").text(basicInfo.user.realName);
		$("#email").text(basicInfo.user.email);
		$("#tel").text(basicInfo.user.tel);
		$("#certificateId").text(basicInfo.certificateId);
		$("#certificateType").text(basicInfo.certificateType);
		$("#estateAddress").text(basicInfo.estateAddress);
		$("#estateType").text(basicInfo.estateType);
		$("#estateUsingStatue").text(basicInfo.estateUsingStatue);
		$("#structure").text(basicInfo.structure);
		$("#acreage").text(basicInfo.acreage);
		$("#estateDtail").modal("show");
		
	});
	/** 房产发布 */
	$("section table").on("click",".btn-success",function(){
		$_modifyNode = $(this).parents("tr");
		var basicInfo = $(this).parents("tr").data("estatebasicinfo");
		$(".estateID").eq(1).text(basicInfo.id);
		$("input[name='estatebasicinfo.id']").val(basicInfo.id);
		
		$("#estatePublic").modal("show");
		
	});
	
	
	
	$("section table").on("click", ".btn-default", function(){
		$_modifyNode = $(this).parents("tr");
		var basicInfo = $(this).parents("tr").data("estatebasicinfo");
		$(".estateID").eq(3).text(basicInfo.id);
		$("#showMap").modal("show");
		$.getJSON("http://api.map.baidu.com/geocoder/v2/?address="+ basicInfo.estateAddress +"&output=json&ak=n7mTxnHfQyF2MG5CN76ZXe7z&callback=?",function(data){
			var point =  new BMap.Point(data.result.location.lng, data.result.location.lat);
			
				map.clearOverlays(); 
				var marker = new BMap.Marker(point);  // 创建标注
				map.addOverlay(marker);              // 将标注添加到地图中
				map.panTo(point);
		}); 
		
	});
	
	
	
	
	$("section table").on("click",".btn-warning",function(){
		var basicInfo = $(this).parents("tr").data("estatebasicinfo");
		$_modifyNode = $(this).parents("tr");
		$(".estateID").eq(2).text(basicInfo.id);
		$("#addPicture").modal("show");
		$("input[type=file]").fileinput("refresh",{
			uploadUrl : baseUrl + "user/json-addEstateFile.do",
			uploadExtraData : {estateBasicId:basicInfo.id} 
		});
	});
		
	$("#file-submit").click(function(){
		$("input[type=file]").fileinput("upload").on("fileuploaded",function(event, data, previewId, index){
			if(data.response.code == 1000){
				sweetAlert({
					title:"成功",
					text:"恭喜，您的文件已上传成功",
					type:"success",
				});
			}else{
				console.log("上传错误",data.data);
				sweetAlert({
					title:"失败",
					text:"系统错误，您的文件已上传失败",
					type:"error",
				});
			}
		});
	});
	
	
	$("#estatePublic .btn-primary").click(function(){
		var $_thisNode = $(this);
		$.ajax({
			url: baseUrl + "user/json-publicLeaseOrSaleInfo.do",
			method:"post",
			data:fromToJson("public-form"),
			success:function(data){
				if(data.code == 1000){
					sweetAlert({
						title:"发布成功",
						text:"您的房产租售信息已发布",
						type:"success",
					});
				$_thisNode.remove();
				}else{
					sweetAlert({
						title:"发布失败",
						text:"系统发生错误",
						type:"error",
					});
				}
			},
			error:function(){
				sweetAlert({
						title:"发布失败",
						text:"连接服务器失败",
						type:"error",
					});
			}
			
			
		});
		$("#estatePublic").modal("hide");
	});
	
	
	
	/* 动态html事件绑定  开始*/
	
	/* 翻页  开始 */
	$(".previous").click(function(){
		if(loadDataDefault.curPage > 1){
			loadDataDefault.curPage--;
			loadDataDefault.data.pageNum = loadDataDefault.curPage;
			loadData();
			if(loadDataDefault.curPage == 1){
				$(this).addClass("disabled");
			}
			if(isEnd && loadDataDefault.curPage == (loadDataDefault.maxSize-1)){
				$(".next").removeClass("disabled");
			}
			
		}
	});
	
	$(".next").click(function(){
		loadDataDefault.curPage++;
		if(isEnd && loadDataDefault.curPage >= loadDataDefault.maxSize){
			loadDataDefault.curPage = loadDataDefault.maxSize;
			$(this).addClass("disabled");
		}
		loadDataDefault.data.pageNum = loadDataDefault.curPage;
		loadData();
		if(loadDataDefault.curPage == 2){
			$(".previous").removeClass("disabled");
		}
	});
	
	/* 翻页  结束 */
	
	
	
	
	
	
	//从from获取数据，转为对象  
	function fromToJson(form) {  
	    var result = {};  
	    var fieldArray = $('#' + form).serializeArray();  
	    for (var i = 0; i < fieldArray.length; i++) {  
	        var field = fieldArray[i];  
	        if (field.name in result) {  
	            result[field.name] += ',' + field.value;  
	        } else {  
	            result[field.name] = field.value;  
	        }  
	    }  
	    return result;  
}  
	
	
	//初始化地图
	function initMap(){
		map = new BMap.Map("estateMap");
		map.centerAndZoom(new BMap.Point(116.331398,39.897445),15);
		map.enableScrollWheelZoom(true);
	}
	
	
	/* 开始操作 */
	
	loadData();
	$("input[type=file]").fileinput({
				language: 'zh', //设置语言
                uploadUrl: "/FileUpload/Upload", //上传的地址
                allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀
		
		
	});
	initMap();
});